package com.wenli.lt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenli.lt.entity.*;
import com.wenli.lt.entity.Collections;
import com.wenli.lt.entity.po.CommentlistPo;
import com.wenli.lt.entity.po.ReportPo;
import com.wenli.lt.mapper.*;
import com.wenli.lt.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lt
 * @since 2021-03-23
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    ImgMapper imgMapper;

    @Autowired
    CommentlistMapper commentlistMapper;

    @Autowired
    CollectionsMapper collectionsMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    ReportlistMapper reportlistMapper;

    @Autowired
    FansMapper fansMapper;

    @Override
    public Map getGoodsInfo(String goodsId,String studentNumber) {
        Goods goods = goodsMapper.selectById(goodsId);
        QueryWrapper<Img> queryWrapper = new QueryWrapper();
        queryWrapper.eq("goodsId",goodsId);
        queryWrapper.select().orderByAsc("createTime");
        List<Img> imgs = imgMapper.selectList(queryWrapper);
        List<CommentlistPo> commentlists = commentlistMapper.selectcommentList(goodsId);
        //学号 查询是否收藏
        List<Collections> collections = collectionsMapper.selectCollectionList( goodsId);
        List<ReportPo> reportlists = reportlistMapper.selectReportList(goodsId);

        Map map = new HashMap();
        map.put("goods",goods);
        map.put("user",userMapper.selectById(goods.getSellerstudentNumber()));
        map.put("goodsImg",imgs);
        //是否关注
        QueryWrapper<Fans> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("focusId",studentNumber);
        queryWrapper1.eq("befocusId",goods.getSellerstudentNumber());
        Fans fans = fansMapper.selectOne(queryWrapper1);
        if (!ObjectUtils.isEmpty(fans)){
            map.put("isFocos",1);

        }else {
            map.put("isFocos",0);
        }
        //是否收藏
        QueryWrapper<Collections> queryWrapper2 = new  QueryWrapper();
        queryWrapper2.eq("goodsId",goodsId);
        queryWrapper2.eq("collectionsStuNum",studentNumber);
        Collections collections1 = collectionsMapper.selectOne(queryWrapper2);
        if (!ObjectUtils.isEmpty(collections1)){
            map.put("isCollection",1);

        }else {
            map.put("isCollection",0);
        }
        map.put("commentList",commentlists);
        map.put("reportList",reportlists);
        map.put("collectionsCount",collections.size());
        int count = reportlistMapper.selectReportListCount(goods.getSellerstudentNumber());
        map.put("reportCount",count);
        return map;
    }

    @Override
    public String saveGoodsInfo(Goods goods) {
       String studentNumber =  goods.getSellerstudentNumber();
       if (!StringUtils.isBlank(studentNumber)){
           User user = userMapper.selectById(studentNumber);
           if (user.getIsRelease() == 1){
               return "2";
           }else {
               String uuid = UUID.randomUUID().toString().replace("-","");
               goods.setGoodsId(uuid);
               goods.setSoldCount(0);
               goods.setIsSoldOut(0);
               int insert = goodsMapper.insertGoods(goods);
               if (insert == 1){
                   return uuid;
               }else {
                   return null;
               }
           }
       }else {
           return "1";
       }
    }

    @Override
    public Boolean removeGoods(String goodsId) {
        int delete = goodsMapper.deleteById(goodsId);
        return delete == 1;
    }

    @Override
    public IPage mangerGoods(String goodsName, Integer orderWay,Integer classification, Long page, Long size) {
        if (orderWay != null){

        if (orderWay == 0 ){
            return goodsMapper.getGoodsListPage(new Page<>(page,size),goodsName,classification,null,null,1);

        }else if (orderWay == 1){
            return goodsMapper.getGoodsListPage(new Page<>(page,size),goodsName,classification,1,null,null);

        }else if (orderWay == 2){
            return goodsMapper.getGoodsListPage(new Page<>(page,size),goodsName,classification,null,1,null);

        }else {
            return goodsMapper.getGoodsListPage(new Page<>(page,size),goodsName,classification,null,null,null);

        }
    }else {
            return goodsMapper.getGoodsListPage(new Page<>(page,size),goodsName,classification,null,null,null);
        }
    }

    @Override
    public IPage selectGoodsInfo(String goodsName, Integer classification, Long page, Long size) {

        return goodsMapper.selectGoodsInfo(new Page<>(page,size),goodsName,classification);
    }
}
